package runze.util.base;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * Action模板
 * @author 王润泽
 * @创建时间 2017年2月15日 下午4:31:37
 */
@SuppressWarnings({ "serial", "unchecked", "rawtypes" })
public class BaseAction<T> extends ActionSupport {
	
	protected String prefix = null;
	protected IService service;
	protected static final String CT_ID = "ctid";	// 用于存储category Id的id，方便异步取出数据
	protected static final String ID_GET = "id";	// 用于存储get的id，方便异步取出数据
	protected static final String PARTSID = "parts_id";
	protected static final String CARID = "car_id";
	
	protected static final String INDEX = "index";
	
	protected static final String SIGN = "sign";

	/**
	 * 用于获取前缀
	 */
	public BaseAction() {
		super();
		Class<T> targetEntity;
		Type genType = getClass().getGenericSuperclass();
		Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
		targetEntity =  (Class<T>)params[0];
		prefix = targetEntity.getSimpleName();
	}
	/********************** 华丽的分割线  ****************************/
	/********************** 跳转结果字符串  ****************************/
	
	protected static final String PARAM_TABLES = "param_tables";	// 获取参数表
	
	/********************** 存入域中的key  ****************************/
	
	/********************** 华丽的分割线  ****************************/
	
	protected static final String S = "s";
	protected static final String CONDITION = "_condition";
	
	// private Response r = new Response();
	
	protected Response list(Query query) {
		List<T> list = service.find(query);
		Object voList = makeVolist(list);
		Object data = voList == null ? list : voList;
		
		query.setMaxPageNo(service.getTotal(query));
		
		putSession(prefix + CONDITION, query);
		
		return r.success(data, query);
	}
	protected Response search(Query query) {
		List<T> list = service.find(query);
		Object voList = makeVolist(list);
		Object data = voList == null ? list : voList;
		
		query.setMaxPageNo(service.getTotal(query));
		
		putSession(prefix + CONDITION, query);
		
		return r.success(data, query);
	}
	protected Response nxPage(Query query) {		// 当下一页超过最大页，返回空
		if (query == null) {
			query = new Query(service.getTotal());
		}
		
		int pageNo = query.getPageNo();
		int maxPageNo = query.getMaxPageNo();
		
		Object data = null;
		if (pageNo != 0 && pageNo < maxPageNo) {
			query.setPageNo( pageNo + 1);	// 增加一页
			
			List<T> list = service.find(query);
			Object voList = makeVolist(list);
			data = voList == null ? list : voList;
		}

		return r.success(data, query);
	}
	protected Response prePage(Query query) {
		if (query == null) {
			query = new Query(service.getTotal());
		}
		int pageNo = query.getPageNo();
		if (pageNo >= 0)
			query.setPageNo(pageNo - 1);	// 减少一页
		
		List<T> list = service.find(query);
		Object voList = makeVolist(list);
		Object data = voList == null ? list : voList;
		
		return r.success(data, query);
	}
	// 原来的
	protected Response nxPage() {		// 当下一页超过最大页，返回空
		// 若没有condition，new一个设置为第二页放入session
		Query query = (Query)getSession(prefix + CONDITION);
		
		int pageNo = query.getPageNo();
		int maxPageNo = query.getMaxPageNo();
		
		Object data = null;
		if (pageNo != 0 && pageNo < maxPageNo) {
			query.setPageNo( pageNo + 1);	// 增加一页
			
			List<T> list = service.find(query);
			Object voList = makeVolist(list);
			data = voList == null ? list : voList;
			
			putSession(prefix + CONDITION, query);
			
		}
		
		return r.success(data, query);
	}
	protected Response prePage() {
		Query query = (Query)getSession(prefix + CONDITION);
		if (query == null) {
			query = new Query(service.getTotal());
		}
		int pageNo = query.getPageNo();
		if (pageNo >= 0)
			query.setPageNo(pageNo - 1);	// 减少一页
		
		List<T> list = service.find(query);
		Object voList = makeVolist(list);
		Object data = voList == null ? list : voList;
		
		putSession(prefix + CONDITION, query);
		
		return r.success(data, query);
	}
	
	// 后台由于直接用query，除了分页，没有其他条件，所以直接使用也无所谓
	protected Response skipPage(Query query) {
		if (query == null) query = new Query();
		
		List<T> list = service.find(query);
		Object voList = makeVolist(list);
		Object data = voList == null ? list : voList;
		
		query.setMaxPageNo(service.getTotal(query));
		
		return r.success(data, query);
	}

	// 登录用到
	protected Response byId() {
		String id_str = (String) getSession(ID_GET);
		if (id_str == null) id_str = "1";
		Integer id = Integer.getInteger(id_str);
		if (id == null) id = 1;
		removeSession(ID_GET);
		Object data = service.get(id);
		putSession(prefix, data);
		
		return r.success(data);
	}
	protected Response byId(int id) {
		if (id == 0) 
			return r.failure("id 不能为空！");
		Object data = service.get(id);
		putSession(prefix, data);
		
		return r.success(data);
	}
	protected Response modify(T t) {
		service.update(t);
		
		return r.success();
	}
	protected Response del(int id) {
		service.del(id);
		
		return r.success();
	}
	protected Response add(T t) {
		if (service.add(t) == 0) return r.failure();
		return r.success();
	}
	
	protected void put(String key, Object value) {
		ActionContext context = ActionContext.getContext();
		context.put(key, value);
	}
	
	protected Object get(String key) {
		ActionContext context = ActionContext.getContext();
		return context.get(key);
	}
	
	protected void putSession(String key, Object value) {
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		session.put(key, value);
	}
	
	protected Object getSession(String key) {
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		return session.get(key);
	}
	
	protected void removeSession(String key) {
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		session.remove(key);
	}
	protected List<? extends IVo> makeVolist(List<T> list) {
		return null;
	}
	
	private Response r = new Response();
}
//protected Response skipPage(Condition condition) {
//Condition c = (Condition) getSession(prefix + CONDITION);
//if (c == null) c = new Condition();
//
//c.setPageNo(condition.getPageNo());
//List<T> list = service.find(c);
//putSession(prefix + CONDITION, c);
//
//m.put(prefix + S, list);
//m.put(prefix + CONDITION, c);
//
//return r.success(m);
//}

